﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Report_Unity.Basic
{
    public static class MethodExtensions
    {
        public static List<object> GetEnumType<T>(this T t)
        {
            Type type = t.GetType();
            FieldInfo[] fields = type.GetFields();
            List<object> list = new List<object>();
            if (fields != null && fields.Length > 0)
            {
                foreach (FieldInfo f in fields)
                {
                    if (f.Name == "value__")
                        continue;
                    int key = f.GetValue(t).GetHashCode();
                    RemarkAttribute attr = f.GetCustomAttribute(typeof(RemarkAttribute)) as RemarkAttribute;
                    string val = attr.GetRemark();
                    list.Add(new { key = key, val = val });
                }
            }
            return list;
        }

        public static string GetPropertyName<T, K>(this Expression<Func<T, K>> expr) where T : class
        {
            var result = "";
            if (expr.Body is UnaryExpression)
                result = ((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;
            else if (expr.Body is MemberExpression)
                result = ((MemberExpression)expr.Body).Member.Name;
            else if (expr.Body is ParameterExpression)
                result = ((ParameterExpression)expr.Body).Type.Name;

            return result;
        }

        public static HttpResponseMessage GetHttpResponseImage(this byte[] bys)
        {
            HttpResponseMessage res = new HttpResponseMessage(HttpStatusCode.OK);
            res.Content = new ByteArrayContent(bys);
            res.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");

            return res;
        }

        public static string HandleListStr(this List<string> list)
        {
            string str = string.Format("'{0}'", string.Join(",", list).Replace(",", "','"));
            return str;
        }


    }
}
